package com.madou.yy.newyear;

import cn.hutool.core.date.ChineseDate;

import javax.swing.*;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

public class TimePanel extends JPanel implements Runnable {
    Color bgColor0 = new Color(242, 173, 46);
    Color bgColor1 = new Color(243, 210, 91);
    Color bgColor2 = new Color(253, 233, 133);
    Color bgColor3 = new Color(194, 112, 41);
    Color bgColor4 = new Color(246, 234, 181);
    Color lineColor = bgColor3;
    Stroke stroke = new BasicStroke(2);
    Stroke stroke2 = new BasicStroke(2,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER,1.0f,new float[]{6F},10f);

    Color font1 = Color.RED;
    Color font2 = new Color(129, 121, 82);
    Color font3 = new Color(44, 33, 14);
    String title1 = "",title2="",nong="",dao="";

    public void paintComponent(Graphics g) {
        // 设置抗锯齿
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        // 最底层背景
        g2d.setColor(bgColor0);
        g2d.fillRect(0,0,getWidth(),getHeight());
        // 第2层背景
        g2d.setColor(bgColor1);
        g2d.fillRoundRect(20,5,getWidth()-40,getHeight()-60,10,10);
        // 第2层背景边框
        g2d.setStroke(stroke);
        g2d.setColor(Color.BLACK);
        g2d.drawRoundRect(20,5,getWidth()-40,getHeight()-60,10,10);
        // 多边形
        int px[] = new int[]{10,50,getWidth()-10,getWidth()-10,10,10,};
        int py[] = new int[]{35,15,15,getHeight()-45,getHeight()-45,35};
        g2d.setColor(bgColor2);
        g2d.fillPolygon(px,py,px.length-1);// 多边形填充
        g2d.setColor(Color.BLACK);
        g2d.drawPolyline(px,py,px.length);// 多边形边
        // 深色 第3层背景+线
        g2d.setColor(bgColor3);
        g2d.fillRoundRect((int)(getWidth()*0.25),8,(int)(getWidth()*0.5),100,10,10);
        g2d.drawRoundRect((int)(getWidth()*0.25),8,(int)(getWidth()*0.5),100,10,10);
        // 第4层背景+虚线
        g2d.setColor(bgColor4);
        g2d.fillRoundRect(40,45,getWidth()-80,getHeight()-110,12,12);
        g2d.setStroke(stroke2);
        g2d.setColor(lineColor);
        g2d.drawRoundRect(40,45,getWidth()-80,getHeight()-110,12,12);
        // 绘制文字 第一排
        Font font = new Font("微软雅黑",Font.BOLD,36);
        g2d.setColor(font1);
        g2d.setFont(font);
        Rectangle2D title1Bounds = font.getStringBounds(title1, g2d.getFontRenderContext());
        g2d.drawString(title1,(int)(getWidth()-title1Bounds.getWidth())/2,100);
        // 绘制文字 第二排：农历
        font = new Font("微软雅黑",Font.BOLD,18);
        g2d.setColor(font2);
        g2d.setFont(font);
        Rectangle2D nongBounds = font.getStringBounds(nong, g2d.getFontRenderContext());
        g2d.drawString(nong,(int)(getWidth()-nongBounds.getWidth())/2,150);
        // 绘制文字 第三排
        font = new Font("微软雅黑",Font.BOLD,18);
        g2d.setColor(font2);
        g2d.setFont(font);
        Rectangle2D title2Bounds = font.getStringBounds(title2, g2d.getFontRenderContext());
        g2d.drawString(title2,(int)(getWidth()-title2Bounds.getWidth())/2,200);
        // 绘制文字 第四排 倒计时
        font = new Font("微软雅黑",Font.BOLD,48);
        g2d.setColor(font3);
        g2d.setFont(font);
        Rectangle2D daoBounds = font.getStringBounds(dao, g2d.getFontRenderContext());
        g2d.drawString(dao,(int)(getWidth()-daoBounds.getWidth())/2,280);
    }

    @Override
    public void run() {
        LocalDate localDate = LocalDate.now();
        localDate = localDate.plusYears(1).withMonth(1).withDayOfMonth(1);
        LocalDateTime localDateTime = localDate.atTime(0, 0, 0, 0);
        while (true){
            ChineseDate chineseDate = new ChineseDate(localDate);
            title1 = String.format("%s %s 年元旦倒计时",localDate.getYear(),chineseDate.getChineseZodiac());
            nong = String.format("元旦时间：%s 农历%s%s %s %s",
                    localDateTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")),
                    chineseDate.getChineseMonthName(),
                    chineseDate.getChineseDay(),
                    localDate.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.CHINESE),
                    chineseDate.getCyclicalYMD()
            );
            title2 = String.format("目前距离%s年元旦还有",localDate.getYear());
            Duration duration = Duration.between( LocalDateTime.now(),localDateTime);
            dao =String.format("%s天%s时%s分%s秒",
                    duration.toDays(),
                    duration.toHours()%24,
                    duration.toMinutes()%60,
                    duration.getSeconds()%60
                    );
            try {
                repaint();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void start() {
        new Thread(this).start();
    }
}
